fileio = {}

-- Find the length of a file
--   filename: file name
-- returns
--   len: length of file
--   asserts on error
function fileio.length_of_file(filename)
    local fh = assert(io.open(filename, "rb"))
    local len = assert(fh:seek("end"))
    fh:close()
    return len
end

-- Return true if file exists and is readable.
function fileio.file_exists(path)
    local file = io.open(path, "rb")
    if file then file:close() end
    return file ~= nil
end

-- Guarded seek.
-- Same as file:seek except throws string
-- on error.
-- Requires Lua 5.1.
function fileio.seek(fh, ...)
    assert(fh:seek(...))
end

-- Read an entire file.
-- Use "a" in Lua 5.3; "*a" in Lua 5.1 and 5.2
function fileio.readall(filename)
    local fh = assert(io.open(filename, "rb"))
    local contents = assert(fh:read(_VERSION <= "Lua 5.2" and "*a" or "a"))
    fh:close()
    return contents
end

-- Write a string to a file.
function fileio.write(filename, contents)
    local fh = assert(io.open(filename, "wb"))
    fh:write(contents)
    fh:flush()
    fh:close()
end

-- Read, process file contents, write.
function fileio.modify(filename, modify_func)
    local contents = readall(filename)
    contents = modify_func(contents)
    write(filename, contents)
end
